//
//  OneAuthorizeCodeViewController.m
//  lTTheOne
//
//  Created by sy on 2025/7/24.
//

#import "OneAuthorizeCodeViewController.h"
#import "OneChatVipSetManager.h"
#import "OneAuthorizeCodeManager.h"
#import <Masonry/Masonry.h>

@interface OneAuthorizeCodeViewController ()<OneAuthorizeCodeViewDelegate>
@property(nonatomic, strong) OneAuthorizeCodeView *authorizeCodeView;
@property (nonatomic,copy) NSString *inputCode;
@end

@implementation OneAuthorizeCodeViewController
- (OneAuthorizeCodeView *)authorizeCodeView{
    if (!_authorizeCodeView) {
        _authorizeCodeView = [[OneAuthorizeCodeView alloc]initWithFrame:CGRectZero withCodeType:self.codeType withState:self.codeState];
        _authorizeCodeView.delegate = self;
    }
    return _authorizeCodeView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSubViews];
    self.authorizeCodeView.backgroundColor = KUIColorFromRGB(0x000000);
    KViewRadius(self.authorizeCodeView, 10);
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(authorizeCodeViewWillDisappear)]) {
        [self.delegate authorizeCodeViewWillDisappear];
    }
}

- (void)initSubViews{
    [self.view addSubview:self.authorizeCodeView];
    [self.authorizeCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.height.mas_equalTo(398);
        make.width.mas_equalTo(270);
    }];
}

#pragma mark - OneAuthorizeCodeViewDelegate
- (void)sureActionCodeType:(authorizeCode)codeType withState:(authorizeState)state withCodeText:(NSString *)inputCode{
    self.inputCode = inputCode;
    
    if (self.isLocalCheck == YES) {
        BOOL result = [[OneAuthorizeCodeManager sharedManager] checkLocalCode:codeType withNewCode:self.inputCode];
        if (self.delegate && [self.delegate respondsToSelector:@selector(authorizeCodeCheckResult:)]) {
            [self.delegate authorizeCodeCheckResult:result];
        }
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else{
        BOOL isOpen;
        if (state == checkCode) {//关闭验证码--验证
            isOpen = NO;
        }else{//打开授权码--重新设置
            isOpen = YES;
        }
        // TODO: - 缺各种模式更新接口
        NSDictionary *tampDic;
        if (codeType == chatCode) {
            tampDic =@{@"chat_pwd_switch":[NSNumber numberWithBool:isOpen],@"chat_pwd":inputCode};
        }else if (codeType == momentCode){
            tampDic =@{@"moment_pwd_switch":[NSNumber numberWithBool:isOpen],@"moment_pwd":inputCode};
        }
        [self updateChatModelSet:tampDic isSeniorFun:YES];
    }
}

- (void)updateChatModelSet:(NSDictionary*)params isSeniorFun:(BOOL)isSenior{
    [OneChatVipSetManager updateChatUserConfig:params withResult:^(BOOL result) {
        if (result == YES) {
            [[OneAuthorizeCodeManager sharedManager]updateCodeAction:self.codeType withCodeStr:(self.codeState == setCode?self.inputCode:@"")];
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(authorizeCodeSetAction:)]) {
            [self.delegate authorizeCodeSetAction:result];
        }
    } withError:^(NSError * _Nonnull error) {
        
    }];
}

@end
